package com.zwk.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.edu.entity.Teacher;
import com.zwk.edu.mapper.TeacherMapper;
import com.zwk.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author zwk
 * @since 2022-08-17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    // 分页获取前台名师列表
    @Override
    public Map<String, Object> getTeacherPageList(Page<Teacher> page) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        teacherMapper.selectPage(page, wrapper);

        // 判断当前页的合法性
        if (page.getCurrent() > page.getPages()){
            page.setCurrent(page.getPages());
            teacherMapper.selectPage(page, wrapper);
        }

        // 取出数据封装到map中
        Map<String, Object> map = new HashMap<>();
        List<Teacher> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext(); // 上一页
        boolean hasPrevious = page.hasPrevious(); // 下一页
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
